//Tue May 24 20:07:48 CDT 2011
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class Coins {
public:
	int need;
	int give;
	Coins(int n, int g) {
		this->need = n;
		this->give = g;
	}
	static bool cmp(const Coins &A, const Coins &B) {
		if (abs(A.need - A.give) < abs(B.need - B.give))
			return true;
		else if ((abs(A.need - A.give) == abs(B.need - B.give)) && A.need
				< B.need)
			return true;
		//		if (A.need < B.need)
		//			return true;
		//		if (A.need == B.need && A.give > B.give)
		//			return true;
		return false;
	}
};

class CoinMachinesGame {
public:
	int maxGames(int coins, vector<int> need, vector<int> give) {
		int N = need.size();
		vector<Coins> v;
		for (int i = 0; i < N; i++)
			v.push_back(Coins(need[i], give[i]));
		sort(v.begin(), v.end(), Coins::cmp);
		int count = 0;
		while (coins > 0 && v.size() > 0) {
			if (coins >= v[0].need) {
				int M = coins / v[0].need;
				count += M;
				coins -= (v[0].need - v[0].give) * M;
				//				cout << v[0].need << endl;
			} else
				v.erase(v.begin());
		}
		return count;
	}
};

